import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import axios from 'axios'
axios.defaults.baseURL = "/api" //"http://localhost:8000"

// 请求拦截器
axios.interceptors.request.use(config => {
    const token = localStorage.getItem('accessToken')
    if (token && !['/register', '/login'].includes(config.url)) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
}, error => {
    return Promise.reject(error)
})


// 响应拦截器
axios.interceptors.response.use(response => {
    return response
}, error => {
    if (error.response && error.response.status === 401) {
        localStorage.removeItem('accessToken')
        window.location.href = '/login'
    }
    return Promise.reject(error)
})

const app = createApp(App)
app.use(router)
app.use(ElementPlus)
app.config.globalProperties.$axios = axios
app.mount('#app')